package com.ys.zero.web;

import java.io.File;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.google.gson.Gson;

import com.ys.zero.bean.Result;
import com.ys.zero.bean.User;
import com.ys.zero.biz.BizException;
import com.ys.zero.dao.UserDao;
import com.ys.zero.dao.UserbackDao;


@WebServlet("/upload/*")
@MultipartConfig


public class UploadServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private static String imagesPath = null;//存储用户头像的路径， 写入数据库
       
   //上传头像
	protected void uploadPortrait(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置编码集
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		Part part = request.getPart("file");
		
		String realpath  = this.getServletContext().getRealPath("/");//获取项目名  Zero
		File file  = new File(realpath);
		realpath = file.getParentFile().getParentFile().getAbsolutePath();
		realpath  += "/webapps/ROOT/img";
		String filename = part.getSubmittedFileName();
		String imagesPath = "/img/" + filename;
		String filepath = realpath + "/" + filename;
//		System.out.println(filepath);
		//imagesPath  = filepath;
		ServletContext context = getServletContext();
		context.setAttribute("imagesPath", imagesPath);
		String webpath = "/" + filename;
//		System.out.println(filename);
		part.write(filepath);
		//得到当前用户信息
		
		
		User user = (User)request.getSession().getAttribute("loginedUser");
//		System.out.println(user.getAccount());
		String json = new Gson().toJson(new Result(1,"上传成功"));
		
		response.getWriter().append(json);
	}

	//上传资源的servlet
	protected void uploadResouce(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Part part = request.getPart("file");
		String resouceName = part.getName();
		System.out.println("资源名："+resouceName);
		String realpath = this.getServletContext().getRealPath("/");//获取项目名称 zero
		File file = new File(realpath);
		realpath = file.getParentFile().getParentFile().getAbsolutePath();
		realpath += "/webapps/ROOT/video";
		String filename = part.getSubmittedFileName();
		String videoPath = "video" + filename;
		String filepath = realpath +"/"+ filename;
		System.out.println("filepath:"+filepath);
		System.out.println("filepath:"+filepath);
		part.write(filepath);
		
//		realpath = file.getParentFile().getParentFile().getAbsolutePath();
//		realpath  += "/webapps/ROOT/img";
//		String filename = part.getSubmittedFileName();
//		String imagesPath = "/img/" + filename;
//		String filepath = realpath + "/" + filename;
////		System.out.println(filepath);
//		//imagesPath  = filepath;
//		ServletContext context = getServletContext();
//		context.setAttribute("imagesPath", imagesPath);
//		String webpath = "/" + filename;
////		System.out.println(filename);
//		part.write(filepath);
		toJson(response, resouceName);
		
		
	}
	protected void submitResource(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String resourceName = request.getParameter("resource_name");
		String ResourceDescription = request.getParameter("Resource_description");
		String ResourceLabel = request.getParameter("Resource_label");
		String  ResourceClassification= request.getParameter("Resource_classification");
		String ResourceForm = request.getParameter("Resource_form");
		System.out.println("resourceName:"+resourceName);
		System.out.println("ResouResourceClassificationrceDescription:"+ResourceDescription);
		System.out.println("ResourceLabel:"+ResourceLabel);
		System.out.println("ResourceClassification:"+ResourceClassification);
		System.out.println("ResourceForm:"+ResourceForm);
		User user = (User)request.getSession().getAttribute("loginedUser");
	    UserbackDao ubdao = new UserbackDao();
	    //判断上传文件的类型(MP4 ， ppt docx ，)
	    String raddress = null;
	    String rcategory = resourceName.substring(resourceName.lastIndexOf('.')+1);
	    if(rcategory.equalsIgnoreCase("jpg") || rcategory.equalsIgnoreCase("jpeg") || rcategory.equalsIgnoreCase("png") || rcategory.equalsIgnoreCase("gif") || rcategory.equalsIgnoreCase("svg")) {
	    	 raddress = "/img/" + resourceName;
	    }else if(rcategory.equalsIgnoreCase("mp4") || rcategory.equalsIgnoreCase("avi") || rcategory.equalsIgnoreCase("mov") || rcategory.equalsIgnoreCase("mov")) {
	    	 raddress = "/video/" + resourceName;
	    }else if(rcategory.equalsIgnoreCase("doc") || rcategory.equalsIgnoreCase("docx")) {
	    	 raddress = "/word/" + resourceName;
	    }else if(rcategory.equalsIgnoreCase("ppt") || rcategory.equalsIgnoreCase("pptx")) {
	    	 raddress = "/ppt/" + resourceName;
	    }else {
	    	
	    	String json  = new Gson().toJson(new Result(0,"上传失败，请核对文件格式"));
			response.getWriter().append(json);
	    }
	    
	    ubdao.insertResource(user.getAccount(),resourceName,ResourceDescription,ResourceLabel,ResourceClassification,ResourceForm,rcategory,raddress);
		String json  = new Gson().toJson(new Result(1,"修改成功"));
		response.getWriter().append(json);
	}
	

}
